<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分tableData类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card style="margin-top: 20px">
      <el-button @click="showAddCateDialog" type="primary">添加分类</el-button>
      <!-- 树状 -->
      <tree-table
        style="margin-top: 20px"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        index-text="#"
        border
        :show-row-hover="false"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            style="color: lightgreen"
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
          ></i>
          <i style="color: red" class="el-icon-error" v-else></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag
            type="success"
            size="mini"
            v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt">
          <el-button type="primary" icon="el-icon-edit" size="mini"
            >编辑</el-button
          >
          <el-button type="danger" icon="el-icon-delete" size="mini"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="querInfo.pagenum"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="querInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 添加用户弹框 -->
    <el-dialog title="添加用户" :visible.sync="addCateDialogVisible">
      <span>
        <el-form
          :model="addCateForm"
          label-width="100px"
          :rules="addCateFormRules"
          ref="addCateFormRef"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>

          <el-form-item label="父级分类">
            <el-cascader
              expand-trigger="hover"
              :options="parentCateList"
              :props="cascaderProps"
              v-model="selectedKeys"
              @change="parentCateChanged"
              clearable
              change-on-select
            ></el-cascader>
          </el-form-item>
        </el-form>
      </span>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户弹框 -->
    <el-dialog title="修改用户" :visible.sync="detailsUser">
      <el-form>
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCategories, getCategory, addCategory } from '@/api/categories'
import { Message } from 'element-ui'
export default {
  name: 'my-categories',
  data () {
    return {
      formLabelWidth: '120px', // 弹框大小
      addCateDialogVisible: false, // 添加弹框显示隐藏
      detailsUser: false, // 修改弹框显示隐藏
      cateList: [{}], // 数据列表渲染
      querInfo: {
        type: 3, // 等级
        pagenum: 1, // 当前页
        pagesize: 5 // 每页几条数据
      },
      total: 0,
      // 列表
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
          width: '400px'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 校验
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 级联
      parentCateList: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectedKeys: []
    }
  },
  created () {
    this.getCateList()
    this.getParentList()
  },

  methods: {
    // 获取商品数据分类
    async getCateList () {
      const res = await getCategories(this.querInfo)
      console.log(res)
      if (res.meta.status !== 200) {
        return Message.error('获取商品分类失败')
      }
      this.cateList = res.data
    },
    // 分页
    handleSizeChange (newSize) {
      console.log(`每页 ${newSize} 条`)
      this.querInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange (newPage) {
      console.log(`当前页: ${newPage}`)
      // 点击哪一页给到当前页
      this.querInfo.pagenum = newPage
      this.getCateList()
    },
    // 添加
    showAddCateDialog () {
      this.getParentList()
      this.addCateDialogVisible = true
    },
    async getParentList () {
      const res = await getCategory()
      console.log(res.meta.status)
      if (res.meta.status !== 200) {
        this.$message.error('获取数据失败')
      }
      this.parentCateList = res.data
    },
    parentCateChanged () {
      // console.log(this.selectedKeys)
      if (this.selectedKeys.length > 3) {
        this.addCateForm.cat_pid =
          this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateForm.cat_level = this.selectedKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    async addCate () {
      this.$refs.addCateFormRef.validate()
      const res = await addCategory(this.addCateForm)
      console.log(res)
      if (res.meta.status !== 201) {
        return this.$message.error('添加失败')
      }
      this.$message.success('添加成功')
      this.getCateList()
      this.detailsUser = false
    },
    addCateDialogClosed () {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    }
  }
}
</script>

<style scoped lang="scss">
.block {
  margin-top: 15px;
}
.box-card {
  margin-top: 20px;
}
.el-input__icon {
  width: 50px;
  height: 38px;
  background-color: #f5f7fa;
  border: 1px solid #dcdfe6;
  margin-right: -58px;
  border-radius: 2px;
}
</style>
